package com.right.health.app.ui.registered;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.magic.basic.utils.AppUtil;
import com.magic.basic.widget.refresh.SwipeListRefreshLayout;
import com.right.health.app.R;
import com.right.health.app.base.BasePresenterActivity;
import com.right.health.app.common.DataCommon;
import com.right.health.app.ui.registered.adapter.HospitalAdapter;

/*
 * 选择医院
 * Created by hopeliao on 2019-09-24
 *
 */
public class ChooseHospitalActivity extends BasePresenterActivity implements SwipeRefreshLayout.OnRefreshListener {

    private static final String EXTRA_KEY = "extra:isChoose";

    private SwipeListRefreshLayout mRefreshView;

    private HospitalAdapter mAdapter;

    private boolean mIsChoose;

    private EditText mSearchEt;
    private TextView mSearchTipsTv;

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.choose_hospital);
    }

    @Override
    public void findView() {
        mSearchEt = (EditText) findViewById(R.id.et_search);
        mSearchTipsTv = (TextView) findViewById(R.id.tv_search_tips);

        mSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String text = s.toString().trim();
                mSearchTipsTv.setVisibility(TextUtils.isEmpty(text) ? View.VISIBLE : View.GONE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        mRefreshView = (SwipeListRefreshLayout) findViewById(R.id.swiperefresh);
        mRefreshView.setOnRefreshListener(this);

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mIsChoose) {
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
//                    bundle.putParcelable(BroadCastKey.DATA, );
                    intent.putExtras(bundle);

                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    startIntent(ChooseDepartmentActivity.class);
                }

            }
        });
        mAdapter = new HospitalAdapter(this);
        listView.setAdapter(mAdapter);
    }

    @Override
    public void initialize() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && bundle.containsKey(EXTRA_KEY)) {
            mIsChoose = bundle.getBoolean(EXTRA_KEY, false);
        }

        mRefreshView.setRefreshing(true);
        mAdapter.setDataSource(DataCommon.getTestDataSource());
    }

    @Override
    public void onRefresh() {

    }

    public static void makeActivity(Activity act, boolean isChoose, int requestCode) {
        Intent intent = new Intent(act, ChooseHospitalActivity.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean(EXTRA_KEY, isChoose);
        intent.putExtras(bundle);

        act.startActivityForResult(intent, requestCode);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppUtil.hideSoftInput(mSearchEt);
    }
}
